package com.amqp.demo.statemachine;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.config.StateMachineFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ConcurrentHashMap;

@RestController
public class webTestController {
//
//    @Autowired
//    private StateMachine<States, Events> stateMachine;

//    @Autowired
//    StateMachineFactory<States, Events> factory;

    @Autowired
    StateMachineFactory<States2, Events2> factory;

    ConcurrentHashMap<String,StateMachine> map = new ConcurrentHashMap<String,StateMachine>();




    @GetMapping("/state")
    public String test(@RequestParam  String key) {
        StateMachine stateMachine = map.get(key);
        if (stateMachine==null){
            stateMachine = factory.getStateMachine();
            map.put(key,stateMachine);
        }
        stateMachine.sendEvent(Events.E1);
        stateMachine.sendEvent(Events.E2);
        return "ok";
    }

    @GetMapping("/state2")
    public String test(@RequestParam  String key,String cmd) {
        StateMachine stateMachine = map.get(key);
        if (stateMachine==null){
            stateMachine = factory.getStateMachine();
            map.put(key,stateMachine);
        }
        stateMachine.sendEvent(Events2.E);
        System.out.println("11123");
        return "ok";
    }

}
